# books/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser


# 管理员数据
class UserInfo(models.Model):
    name = models.CharField(verbose_name="用户名", max_length=20)
    pwd = models.CharField(verbose_name="密码", max_length=20)

    role_choice = (
        ("admin", "管理员"),
        ("manager", "经理"),
        ("user", "用户"),
    )

    role = models.CharField(verbose_name="角色", max_length=20, choices=role_choice, default='user')


# 生产厂商
class Manufacturer(models.Model):
    name = models.CharField(max_length=100, verbose_name="厂商名称")  # 厂商名称
    phone = models.CharField(max_length=20, verbose_name="厂商电话")  # 厂商电话
    address = models.CharField(max_length=200, verbose_name="厂商地址")  # 厂商地址

    def __str__(self):
        return self.name


# 药品数据
class Drug(models.Model):
    drug_name = models.CharField(verbose_name="药品名称", max_length=100)  # 药品名称
    drug_inprice = models.DecimalField(verbose_name="药品进价", max_digits=10,
                                       decimal_places=2)  # 药品价格，使用DecimalField以精确存储货币
    drug_outprice = models.DecimalField(verbose_name="药品售价", max_digits=10,
                                        decimal_places=2)  # 药品价格，使用DecimalField以精确存储货币
    drug_type_choice = (
        ('GRANULE', '颗粒剂'),
        ('TABLET', '片剂'),
        ('CAPSULE', '胶囊'),
        ('DECOCTION_EXTRACT', '煎膏剂'),
        ('PILLET', '滴丸剂'),
        ('TEMPORARY', '暂定'),
    )
    drug_type = models.CharField(verbose_name="药品类型", max_length=30, choices=drug_type_choice,
                                 default='暂定')  # 药品类型

    def __str__(self):
        return self.drug_name


# 库存
class Stock(models.Model):
    stock_quantity = models.IntegerField(verbose_name="库存量")
    address = models.CharField(verbose_name="仓储地址", max_length=255)
    drug_name = models.CharField(verbose_name="药品名称", max_length=100)  # 药品名称
    # 库存预警（可能是一个标志，例如“低”、“正常”等）
    sale_status_choice = (
        ("low", "过低"),
        ("normal", "正常"),
    )
    stock_warning = models.CharField(verbose_name="库存预警", max_length=20, choices=sale_status_choice, default='正常')

    def __str__(self):
        return self.stock_quantity


# 购买者
class Buyer(models.Model):
    name = models.CharField(max_length=100, verbose_name='购买者姓名')
    gender = models.CharField(max_length=10, verbose_name='购买者性别')
    phone = models.CharField(max_length=15, verbose_name='购买者电话')

    def __str__(self):
        return self.name


class Sales(models.Model):
    drug_name = models.CharField(max_length=255, verbose_name='药品名称')
    sale_date = models.DateField(verbose_name='销售日期')
    sale_discount = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='销售折扣')
    quantity = models.IntegerField(verbose_name='销售数量')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='总金额')
    buyer = models.CharField(verbose_name="购买人", max_length=100, blank=True, null=True)

    def __str__(self):
        return self.drug_name


class Supply(models.Model):
    drug_name = models.CharField(max_length=255, verbose_name='药品名称')
    manufacturer_name = models.CharField(max_length=255, verbose_name='厂商名称')
    supply_date = models.DateField(verbose_name='供货日期')
    quantity = models.IntegerField(verbose_name='供货数量')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='总金额')

    def __str__(self):
        return self.drug_name


class DrugTreatment(models.Model):
    drug_name = models.CharField(max_length=255, verbose_name='药品名称')
    drug_quantity = models.IntegerField(verbose_name='药品数量')
    treatment_date = models.DateField(verbose_name='处理日期')
    treatment_method = models.CharField(max_length=255, verbose_name='处理方式')
    treatment_reseon = models.CharField(max_length=255, verbose_name='处理原因')
    treated_by = models.CharField(max_length=255, verbose_name='处理人')

    def __str__(self):
        return f"{self.drug_name} - {self.treatment_date}"
